<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>类别列表</span>
        <el-button type="text" autofocus @click="addDialog = true">添加分类</el-button>
      </div>
      <!-- {{ '列表内容 ' + o }} -->
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column prop="id" label="Id" width="150"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽 -->
          <template v-slot="scope">
            <el-button size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog
      :before-close="cancelCategory"
      title="添加分类"
      :visible.sync="addDialog"
      width="500px"
    >
      <el-form :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelCategory">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加编辑对话框 -->
    <el-dialog title="修改分类" :visible.sync="editDialog" width="500px">
      <el-form ref="etidForm" :model="etidData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="etidData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="etidData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryAPI, addCategoryAPI, delCategoryAPI, updateCategoryAPI } from '@/api/category'
export default {
  data() {
    return {
      // 数据留空
      list: [],
      // 控制添加弹窗的数据项
      addDialog: false,
      // 控制编辑弹窗的数据项
      editDialog: false,
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      etidData: {
        cate_name: '',
        cate_alias: '',
        id: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '分类名必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '长度1~15位的字母数字组合', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取分类
    async getCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    addCategory() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message) // 提示
          // 重置表单
          this.$refs.addForm.resetFields()
          // 关闭弹窗
          this.addDialog = false
          // 重新获取分类列表数据
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 显示添加对话框的方法
    cancelCategory() {
      // 重置表单
      this.$refs.addForm.resetFields()
      // 关闭弹窗
      this.addDialog = false
      // 重新获取分类列表数据
      this.getCategory()
    },
    // 显示编辑对话框的方法
    showEditDialog(row) {
      this.editDialog = true
      // console.log(row)
      this.etidData = { ...row }
    },
    // 编辑确认按钮
    updateCategory() {
      this.$refs.etidForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCategoryAPI(this.etidData)
        if (res.code === 0) {
          this.$message.success(res.message) // 提示
          // 关闭弹窗
          this.editDialog = false
          // 重新获取分类列表数据
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },

    // 删除
    del(id) {
      // console.log(id)
      this.$confirm('确定要删除?', '提示', {
        // confirmButtonText: '确定',
        // cancelButtonText: '取消',
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            // console.log(id)
            // 说明用户点击了确定，这个时候可以调用 API方法进行删除了
            const { data: res } = await delCategoryAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getCategory() // 调用获取分类的方法，重新获取数据，重新渲染到页面
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    }
  },

  created() {
    this.getCategory()
  }
}
</script>
<style lang="less">
.clearfix {
  display: flex;
  justify-content: space-between;
  align-content: center;
}
// .el-input {
//   width: 300px;
// }
</style>
